package com.lxy.htmlparse.parse;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lxy.htmlparse.dao.PicInfoDao;
import com.lxy.htmlparse.empty.PicMinInfo;
import com.lxy.htmlparse.model.SinaBaseResponse;
import com.lxy.htmlparse.model.SinaHtmlResponse;
import com.lxy.htmlparse.utils.HttpClient;
import com.lxy.htmlparse.utils.MapUrlParamsUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author: lxy
 * @Date: 2019/6/19 12:38
 * @Desc: describe this class
 */
@Component
public class WeiboHmtlParse {
    private static Logger logger = LoggerFactory.getLogger(WeiboHmtlParse.class);

    @Autowired
    private HttpClient httpClient;
    @Autowired
    private PicInfoDao picInfoDao;

    @Value("${http.weiboApiUrl}")
    private String weiboApiUrl;

    private static Map<String, Object> paramMap = new HashMap<>();

    public void parse(String params) throws Exception {
        if (params.contains("?")) {
            String[] split = params.split("\\?");
            params = split[1];
        }
        paramMap = MapUrlParamsUtils.getUrlParams(params, paramMap);
        if (paramMap.size() == 0) {
            logger.info("请求参数为空！结束查询");
            return;
        }
        Thread.sleep(2 * 1000);
        String ret = httpClient.doGet(weiboApiUrl, paramMap);
        if (StringUtils.isBlank(ret)) {
            logger.info("微博请求接口返回信息为空，结束查询");
            return;
        }
        String page = (String) paramMap.get("page");
        if (StringUtils.isBlank(page)) {
            page = "1";
        }

        String nextPageParam = parseData(ret, Integer.parseInt(page));
        if (null != nextPageParam) {
            parse(nextPageParam);
        }
        logger.info(">>>>>>>>>>>>>>>>>>解析完成<<<<<<<<<<<<<<<<<");
    }


    public String parseData(String responseStr, Integer pageIndex) {
        logger.info("解析第{}页数据,返回信息:{}", pageIndex, responseStr);
        SinaBaseResponse<SinaHtmlResponse> response = JSONObject.parseObject(responseStr, new TypeReference<SinaBaseResponse<SinaHtmlResponse>>() {
        });
        if (StringUtils.equals("100000", response.getCode())) {
            SinaHtmlResponse htmlData = response.getData();
            Document doc = Jsoup.parse(htmlData.getHtml());
            //获取body中的内容
            Element body = doc.body();
            //获取评论列表
            Elements lis = body.getElementsByAttributeValue("action-type", "comment_media_img");
            Iterator i = lis.iterator();
            while (i.hasNext()) {
                Element li = (Element) i.next();
                Elements imgs = li.getElementsByTag("img");
                Element img = imgs.get(0);
                String src = img.attr("src");
                logger.info("获取到图片url：{}", src);
                PicMinInfo info = new PicMinInfo();
                info.setMinUrl(src);
                info.setPageIndex(pageIndex);
                picInfoDao.save(info);
            }

            //获取下一页数据
            Elements more_comment = body.getElementsByAttributeValue("action-type", "click_more_comment");
            if (more_comment.size() == 0) {
                more_comment = body.getElementsByAttributeValue("node-type", "comment_loading");
                if (more_comment.size() == 0) {
                    more_comment = body.getElementsByClass("next");
                    if (more_comment.size() == 0) {
                        logger.info("已经到达最后一页！");
                        return null;
                    } else {
                        Element element = more_comment.get(0);
                        Elements nextPages = element.getElementsByTag("span");
                        Element nextPage = nextPages.get(0);
                        String nextPageParam = nextPage.attr("action-data");
                        logger.info("下一页请求参数:{}", nextPageParam);
                        return nextPageParam;
                    }
                }

            }
            Element nextPage = more_comment.get(0);
            String nextPageParam = nextPage.attr("action-data");
            logger.info("下一页请求参数:{}", nextPageParam);
            return nextPageParam;
        } else {
            logger.error("获取微博页面代码失败，错误编码:{},错误信息:{}", response.getCode(), response.getMsg());
            return null;
        }
    }
   /* public String parseData2(String responseStr, Integer pageIndex) {
        logger.info("解析第{}页数据,返回信息:{}", pageIndex, responseStr);
        SinaBaseResponse<SinaHtmlResponse> response = JSONObject.parseObject(responseStr, new TypeReference<SinaBaseResponse<SinaHtmlResponse>>() {
        });
        if (StringUtils.equals("100000", response.getCode())) {
            logger.info("获取微博页面数据成功");
            SinaHtmlResponse htmlData = response.getData();
            Document doc = Jsoup.parse(htmlData.getHtml());
            //获取body中的内容
            Element body = doc.body();
            //获取评论列表
            Elements elements = body.getElementsByAttributeValue("node-type", "comment_list");
            if (null == elements || elements.size() == 0) {
                logger.warn("没有需要解析的数据！");
                return null;
            }
            Element comment = elements.get(0);
            Elements rootComment = comment.getElementsByClass("list_li");
            if (null == rootComment || rootComment.size() == 0) {
                logger.warn("没有需要解析评论列表据！");
                return null;
            }
            Iterator it = rootComment.iterator();
            while (it.hasNext()) {
                Element singleComent = (Element) it.next();
                Elements replywraps = singleComent.getElementsByAttributeValue("node-type", "replywrap");
                if (replywraps.size() == 0) {
                    continue;
                }
                Element replywrap = replywraps.get(0);
                Elements comment_media_prevs = replywrap.getElementsByAttributeValue("node-type", "comment_media_prev");
                if (comment_media_prevs.size() == 0) {
                    continue;
                }
                Element comment_media_prev = comment_media_prevs.get(0);
                Elements lis = comment_media_prev.getElementsByAttributeValue("action-type", "comment_media_img");
                if (lis.size() == 0) {
                    continue;
                }
                Iterator i = lis.iterator();
                while (i.hasNext()) {
                    Element li = (Element) i.next();
                    Elements imgs = li.getElementsByTag("img");
                    Element img = imgs.get(0);
                    String src = img.attr("src");
                    logger.info("获取到图片url：{}", src);
                    PicMinInfo info = new PicMinInfo();
                    info.setMinUrl(src);
                    info.setPageIndex(pageIndex);
                    picInfoDao.save(info);
                }
            }

            //获取下一页数据
            Elements more_comment = body.getElementsByAttributeValue("action-type", "click_more_comment");
            if (more_comment.size() == 0) {
                more_comment = body.getElementsByAttributeValue("node-type", "comment_loading");
                if (more_comment.size() == 0) {
                    more_comment = body.getElementsByClass("next");
                    if (more_comment.size() == 0) {
                        logger.info("已经到达最后一页！");
                        return null;
                    }else {
                        Element element = more_comment.get(0);
                        Elements nextPages = element.getElementsByTag("span");
                        Element nextPage = nextPages.get(0);
                        String nextPageParam = nextPage.attr("action-data");
                        logger.info("下一页请求参数:{}", nextPageParam);
                        return nextPageParam;
                    }
                }

            }
            Element nextPage = more_comment.get(0);
            String nextPageParam = nextPage.attr("action-data");
            logger.info("下一页请求参数:{}", nextPageParam);
            return nextPageParam;
        } else {
            logger.error("获取微博页面代码失败，错误编码:{},错误信息:{}", response.getCode(), response.getMsg());
            return null;
        }
    }*/
}
